package model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import view.ContactForm;
import controleur.ActionContact;
import controleur.ActionVue;
import controleur.MainControleur;

public class GestionAddress {

	
	private ArrayList<String> listcontact;
	public static ArrayList<String> cont1;
	public Contact contact;
	
	public GestionAddress() {
		
	}
	
	
	public ArrayList<String> addContact(ArrayList<ArrayList<String>> Listcontact, Contact contact) {
		
		// On ajoute le contact dans l'arraylist
		listcontact = new ArrayList<String>();
		listcontact.add(contact.getFname());
		listcontact.add(contact.getLname());
		listcontact.add(contact.getGroup());
		listcontact.add(contact.getHomeAdd());
		listcontact.add(contact.getCompanyAdd());
		listcontact.add(contact.getHomePhone());
		listcontact.add(contact.getMobilePhone());
		listcontact.add(contact.getProfessionalPhone());
		listcontact.add(contact.getEmailPerso());
		listcontact.add(contact.getEmailProf());
		listcontact.add(contact.getHomePage());
		listcontact.add(contact.getPicture());

		Listcontact.add(listcontact);
		String key = listcontact.get(0) + listcontact.get(1) + listcontact.get(8);
		MainControleur.dataHM.put(key, listcontact);
		return listcontact;
	}
	
	public ArrayList<String> deleteContact(ArrayList<ArrayList<String>> Listcontact, String Fname, 
			String Lname, String EmailPerso) {
		
		
		for (int i=0; i<Listcontact.size(); i++)
		{
			if (Listcontact.get(i).get(0).equals(Fname) && Listcontact.get(i).get(1).equals(Lname)
					&& Listcontact.get(i).get(8).equals(EmailPerso))
			{
				listcontact = Listcontact.get(i);
				Listcontact.remove(i);
				
				FileManagement.deleteLine(MainControleur.filename, i+1);
				
				break;
			}
		}

		return listcontact;
	}
	
	public ArrayList<String> searchContact(ArrayList<ArrayList<String>> Listcontact, String Fname, 
			String Lname, String EmailPerso) {
		
		String key1 = Fname+Lname+EmailPerso;
		System.out.println(key1);
		System.out.println("hahaa HM");
		System.out.println(MainControleur.dataHM);
		System.out.println("hahaa HM2");
		
		if (MainControleur.dataHM.containsKey(key1))
		{
			listcontact = MainControleur.dataHM.get(key1);
		}
		
		/*for (int i=0; i<Listcontact.size(); i++)
		{
			if (Listcontact.get(i).get(0).equals(Fname) && Listcontact.get(i).get(1).equals(Lname)
					&& Listcontact.get(i).get(8).equals(EmailPerso))
			{
				listcontact = Listcontact.get(i);
				break;
			}
		}*/

		return listcontact;
	}
	
	public ArrayList<String> editContact(ArrayList<ArrayList<String>> Listcontact, String Fname, 
			String Lname, String EmailPerso) {
		
		cont1 = searchContact(Listcontact, Fname, Lname, EmailPerso);
		String cols1[] = new String[ActionVue.cols.length];
    	/*for (int i=0; i<ActionVue.cols.length; i++)
    	{
    		cols1[i]=cont1.get(i);
    		System.out.println(ActionVue.cols[i]);
    	}*/
    	
    	//cont = new ContactForm();
        //cont.Menu(cols1);
        
    	ActionVue.cont = new ContactForm();
    	ActionVue.cont.Menu(ActionVue.cols/*cols1*/);
        ActionContact actioncont = new ActionContact(ActionVue.cont);
        
        deleteContact(Listcontact, Fname, Lname, EmailPerso);
        
        ActionVue.cont.getJButtonValidate().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            	
            	if(e.getSource() == ActionVue.cont.getJButtonValidate())
            		
            		contact = new Contact(ActionVue.cont.getJTextFieldFname().getText(),
            				ActionVue.cont.getJTextFieldLname().getText(),
            				ActionVue.cont.getJTextFieldGroup().getText(),
            				ActionVue.cont.getJTextFieldAddressH().getText(),
            				ActionVue.cont.getJTextFieldAddressC().getText(),
            				ActionVue.cont.getJTextFieldPhoneH().getText(),
            				ActionVue.cont.getJTextFieldPhoneM().getText(),
            				ActionVue.cont.getJTextFieldPhoneP().getText(),
            				ActionVue.cont.getJTextFieldEmailPers().getText(),
            				ActionVue.cont.getJTextFieldEmailProf().getText(),
            				ActionVue.cont.getJTextFieldWebPage().getText(),
            				ActionVue.cont.getJTextFieldBrowseIMG().getText()
			    		);
            		addContact(MainControleur.data, contact);
            		ContactForm.jframe1.setVisible(false);
            }
        });
    	
		return cont1;
	}
	
	
}
